package com.xiaoxiong.text;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class HttpURLConnectionText extends Activity implements OnClickListener {

	private final static int SUCCESS = 1;
	private final static int FAILE = 0;
	private TextView result;
	private Button send;
	private LoadingDialog loadingDialog;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initData();
	}

	private void initData() {
		mContext = this;
		result = (TextView) findViewById(R.id.result);
		send = (Button) findViewById(R.id.send);
		send.setOnClickListener(this);
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			String response;
			if(loadingDialog != null && loadingDialog.isShowing()){
				loadingDialog.dismiss();
			}
			switch (msg.what) {
			case SUCCESS:
				response = msg.obj.toString();
				result.setText(response);
				break;
			case FAILE:
				response = msg.obj.toString();
				result.setText(response);
				break;
			}
		};
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.send:
			loadingDialog = new LoadingDialog(mContext);
			loadingDialog.setCancel(false);
			loadingDialog.show();
			startSend();
			break;
		}
	}

	private void startSend() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				HttpURLConnection connection = null;
				try {
					URL url = new URL("http://www.baidu.com/");
					// 调用URL的openConnection()方法，得到HttpURLConnection的实例
					connection = (HttpURLConnection) url.openConnection();
					// 设置HTTP请求方法为GET，一般GET为从服务器获取数据，POST为提交数据给服务器
					connection.setRequestMethod("GET");
					// 设置连接超时的毫秒数
					connection.setConnectTimeout(8000);
					// 设置读取超时的毫秒数
					connection.setReadTimeout(8000);
					// 获取服务器返回的输入流
					InputStream in = connection.getInputStream();
					// 对获取到的输入流进行读取
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(in));
					String response = "", line;
					while ((line = reader.readLine()) != null) {
						response += line;
					}
					//获取成功，发送成功信息
					Message message = new Message();
					message.what = SUCCESS;
					message.obj = response;
					mHandler.sendMessage(message);
				} catch (Exception e) {
					//发送失败信息
					Message message = new Message();
					message.what = FAILE;
					message.obj = "请求失败";
					mHandler.sendMessage(message);
					e.printStackTrace();
				} finally {
					if (connection != null) {
						//如果connection为null，则关闭它
						connection.disconnect();
					}
				}
			}

		}).start();
	}

}
